package com.nsu.tly.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nsu.tly.entity.Goods;
import com.nsu.tly.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

/**
 * @Description:
 * @Author: Elder Tang
 * @Date: 2021/7/23 9:54
 */

@Controller
@RequestMapping("/goods")
public class GoodsController {
    //测试提交
    //zxh测试提交
//用户A提交至远程仓库
    //用户B修改
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/listGoods")
    public String listGoods(Model model){
        List<Goods> goodsList=goodsService.listGoods();
        model.addAttribute("gList",goodsList);
        return "/client/goodsList";
    }


    @RequestMapping("/showGoods")
    public String showGoods(Model model, @RequestParam(value = "page", defaultValue = "1") Integer page){
        //获取指定页数据，大小为8
        PageHelper.startPage(page, 7);
        //紧跟的第一个select方法被分页
        List<Goods> goodsList=goodsService.listGoods();
        //使用PageInfo包装数据
        PageInfo pageInfo = new PageInfo(goodsList, 5);
        model.addAttribute("pageInfo", pageInfo);
        return "/admin/goods/list";
    }









    @RequestMapping("/add")
    public void addGoods(Goods goods, @RequestParam("goodsHomepic1") List<MultipartFile> fileList) {
        //上传文件
        if (!fileList.isEmpty() && fileList.size() > 0) {
            //循环输出上传的文件
            //用于存储商品主图（第一张图片）
            String homePic = "";
            //用于存储商品轮播图（第一张除外的剩余图片）
            StringBuffer carouselPic = new StringBuffer();

            int i = 0;
            for (MultipartFile file : fileList) {
                // 获取上传文件的原始名称
                String originalFilename = file.getOriginalFilename();
                // 设置上传文件的保存地址目录
                String dirPath = "D:\\local_img";
                File filePath = new File(dirPath);
                // 如果保存文件的地址不存在，就先创建目录
                if (!filePath.exists()) {
                    filePath.mkdirs();
                }
                // 使用UUID重新命名上传的文件名称(上传人_uuid_原始文件名称)
                String newFilename = UUID.randomUUID() + "_" + originalFilename;
                File savePath = new File(dirPath + File.separator + newFilename);
                try {
                    // 使用MultipartFile接口的方法完成文件上传到指定位置
                    file.transferTo(savePath);
                    if (i == 0) {
                        homePic = "/local_img/" + newFilename;
                        i++;
                    } else {
                        String tmpPic = "/local_img/" + newFilename;
                        carouselPic.append(tmpPic);
                        carouselPic.append(";");
                        i++;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            goods.setGoodsHomepic(homePic);
            goods.setGoodsCarouselpics(carouselPic.toString());
        }
        goodsService.addGoods(goods);
    }
}

